package com.yuju.rpt.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.rpt.dao.ICheckinCustomTypeDao;
import com.yuju.rpt.dao.entity.CheckinCustomType;
import com.yuju.rpt.enums.CheckinCustomTypeReturnCodeEnum;
import com.yuju.rpt.enums.ReportTypeEnum;
import com.yuju.rpt.model.CheckinCustomTypeDetailModel;
import com.yuju.rpt.model.CheckinCustomTypeModel;
import com.yuju.rpt.service.ICheckinCustomTypeDetailService;
import com.yuju.rpt.service.ICheckinCustomTypeService;
import com.yuju.util.Utils;

/**
 * 类名：CheckinCustomTypeServiceImpl  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2018年2月5日 下午4:49:03  <br />
 * @version : 1.1 <br />
 */
@Service
public class CheckinCustomTypeServiceImpl extends AbstractBaseService<CheckinCustomTypeModel, CheckinCustomType, String> implements ICheckinCustomTypeService {
	
	@Autowired
	private ICheckinCustomTypeDao checkinCustomTypeDao;
	
	@Autowired
	private ICheckinCustomTypeDetailService checkinCustomTypeDetailService;
	
	@Override
	public IBaseDao<CheckinCustomTypeModel, CheckinCustomType, String> getDao() {
		return checkinCustomTypeDao;
	}

	@Override
	public List<CheckinCustomTypeModel> getCheckinStatByCustomType(CheckinCustomTypeModel checkinCustomTypeModel) {
		return checkinCustomTypeDao.getCheckinStatByCustomType(checkinCustomTypeModel);
	}

	@Override
	public int saveCheckinCustomTypeRpt(Date businessDate) {
		CheckinCustomTypeModel checkinCustomTypeModel = new CheckinCustomTypeModel();
		List<CheckinCustomTypeModel> list = this.getCheckinStatByCustomType(checkinCustomTypeModel);
		CheckinCustomTypeDetailModel checkinCustomTypeDetailModel = new CheckinCustomTypeDetailModel();
		checkinCustomTypeDetailModel.setGmtCreate(businessDate);
		List<CheckinCustomTypeDetailModel> detailList = checkinCustomTypeDetailService.getCheckinStatDetailByCustomType(checkinCustomTypeDetailModel);
		for (CheckinCustomTypeModel model : list) {
			model.setId(Utils.getUUID());
			model.setBusinessDate(businessDate);
			model.setBeginTime(businessDate);
			model.setEndTime(new Date(businessDate.getTime()+(long)24*60*60*1000));
			model.setType(ReportTypeEnum.REPORTTYPE_DAY.getValue());
			Date date = new Date();
			model.setGmtCreate(date);
			model.setGmtModified(date);
			if(this.save(model)<1){
				throw new BusinessException(CheckinCustomTypeReturnCodeEnum.CHECKINCUSTOMTYPE_SAVE_FAILURE.getCode(),
						CheckinCustomTypeReturnCodeEnum.CHECKINCUSTOMTYPE_SAVE_FAILURE.getDesc());
			}
			BigDecimal kfSaleSum = new BigDecimal(0);
			for (CheckinCustomTypeDetailModel detailModel : detailList) {
				if(model.getHotelId().equals(detailModel.getHotelId())){
					kfSaleSum = kfSaleSum.add(detailModel.getKfSaleSum());
				}
			}
			
			for (CheckinCustomTypeDetailModel detailModel : detailList) {
				if(model.getHotelId().equals(detailModel.getHotelId())){
					detailModel.setId(Utils.getUUID());
					detailModel.setGroupId(model.getGroupId());
					detailModel.setParentId(model.getId());
					if(model.getKfRoomSum() == 0){
						detailModel.setKfRateZf(new BigDecimal(0));
					}else{
						detailModel.setKfRateZf(detailModel.getKfSaleSum()
								.divide(new BigDecimal(model.getKfRoomSum()), 4, BigDecimal.ROUND_HALF_UP));
					}
					if(kfSaleSum.compareTo(new BigDecimal(0))==0){
						detailModel.setRate(new BigDecimal(0));
					}else{
						detailModel.setRate(detailModel.getKfSaleSum()
								.divide(kfSaleSum, 4, BigDecimal.ROUND_HALF_UP));
					}
					if(detailModel.getKfSaleSum().compareTo(new BigDecimal(0))==0 || Utils.isNull(detailModel.getKfSaleSum())){
						detailModel.setKfAverage(new BigDecimal(0));
					}else{
						detailModel.setKfAverage(detailModel.getYsFfSum()
								.divide(detailModel.getKfSaleSum(), 2, BigDecimal.ROUND_HALF_UP));
					}
					detailModel.setGmtCreate(date);
					detailModel.setGmtModified(date);
					if(checkinCustomTypeDetailService.save(detailModel)<1){
						throw new BusinessException(CheckinCustomTypeReturnCodeEnum.CHECKINCUSTOMTYPE_SAVE_FAILURE.getCode(),
								CheckinCustomTypeReturnCodeEnum.CHECKINCUSTOMTYPE_SAVE_FAILURE.getDesc());
					}
				}
			}
			
		}
		
		return 1;
	}

	/**
	 * 功能: 查找报表信息<br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2018年2月6日 下午2:32:17<br/>
	 * 
	 * @param checkinCustomTypeModel
	 * @return <br/>
	 * @see com.yuju.rpt.service.ICheckinCustomTypeService#getRpt(com.yuju.rpt.model.CheckinCustomTypeModel)
	 */
	@Override
	public Map<String, Object> getRpt(CheckinCustomTypeModel checkinCustomTypeModel) {
		CheckinCustomTypeModel model = this.find(checkinCustomTypeModel);
		if(Utils.isNull(model)){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		CheckinCustomTypeDetailModel checkinCustomTypeDetailModel = new CheckinCustomTypeDetailModel();
		checkinCustomTypeDetailModel.setParentId(model.getId());
		List<CheckinCustomTypeDetailModel> list = checkinCustomTypeDetailService.list(checkinCustomTypeDetailModel);
		if(Utils.isNull(list)){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		Map<String,Object> map = new HashMap<>();
		map.put("model", model);
		map.put("list", list);
		return map;
	}
	
	

}
